//
//  DMCCycleImages.m
//  16-UICollectionViewCycleImage
//
//  Created by dllo on 16/8/4.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "DMCCycleImages.h"
#define kCollectionViewCellReuse @"reuse"
@interface DMCCycleImages ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) UICollectionView *cycleCollectionView;
@property (nonatomic, strong) UIPageControl *pageControl;

@end

@implementation DMCCycleImages

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor redColor];
        [self setUp];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
       self.backgroundColor = [UIColor redColor];
        [self setUp];
//        NSLog(@"%lf", self.frame.size.width);
    }
    return self;
}
- (void)setUp {
    [self createCollectionView];
    [self createPageControl];
}
- (void)createCollectionView {
    _layout = [[UICollectionViewFlowLayout alloc] init];
    _layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _layout.minimumLineSpacing = 0;
    _layout.minimumInteritemSpacing = 0;
    _cycleCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout: _layout];
    _cycleCollectionView.delegate = self;
    _cycleCollectionView.dataSource = self;
    _cycleCollectionView.pagingEnabled = YES;
    [_cycleCollectionView registerClass:[CDMCycleImageCell class] forCellWithReuseIdentifier:kCollectionViewCellReuse];
    [self addSubview:_cycleCollectionView];
}
#pragma mark - CollectionView Datasource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _array.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CDMCycleImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewCellReuse forIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0f green:arc4random() % 256 / 255.0f blue:arc4random() % 256 / 255.0f alpha:1.0];
    cell.cycleImage.image = [UIImage imageNamed:_array[indexPath.row]];
    return cell;
}
- (void)createPageControl {
    
}
- (void)layoutSubviews {
    [super layoutSubviews];
    NSLog(@"%lf", self.frame.size.width);
    _cycleCollectionView.frame = self.bounds;
    _layout.itemSize = self.bounds.size;
    [_cycleCollectionView setContentOffset:CGPointMake(self.bounds.size.width, 0)];

}
//- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
//    if (scrollView.contentOffset.x == 0) {
//        //        [_cycleCollectionView setContentOffset:CGPointMake(_cycleCollectionView.bounds.size.width * (_array.count - 2), 0)];
//        [_cycleCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:(_array.count - 2) inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
//    } else if (scrollView.contentOffset.x == _cycleCollectionView.bounds.size.width * (_array.count - 1)) {
//        //        [_cycleCollectionView setContentOffset:CGPointMake(_cycleCollectionView.bounds.size.width, 0)];
//        [_cycleCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:1 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
//    }
//
//}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
//#warning 直接判断等与0有啥问题
    if (scrollView.contentOffset.x <= _cycleCollectionView.bounds.size.width / 2) {
//        [_cycleCollectionView setContentOffset:CGPointMake(_cycleCollectionView.bounds.size.width * (_array.count - 2), 0)];
        [_cycleCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:(_array.count - 2) inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
    } else if (scrollView.contentOffset.x == _cycleCollectionView.bounds.size.width * (_array.count - 1)) {
//        [_cycleCollectionView setContentOffset:CGPointMake(_cycleCollectionView.bounds.size.width, 0)];
        [_cycleCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:1 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
    }
    
}

- (void)setArray:(NSArray *)array {
    _array = array;
    [_cycleCollectionView reloadData];
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end


@implementation CDMCycleImageCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.cycleImage = [[UIImageView alloc] initWithFrame:self.bounds];
        [self.contentView addSubview:_cycleImage];
    }
    return self;
}

@end
